package lib.persistence.commands;

import global.ConfigData;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import lib.persistence.NotFoundException;
import lib.persistence.commands.GetRow;
import models.*;

public class GetModelsBasedOnDate
{		
	//the following functions get records of each type between two dates and cast it back to it's "model" type.
		public ArrayList<Event> getEvent(int limit, String dateHigh, String dateLow)throws SQLException, IOException, NotFoundException, ClassNotFoundException 
		{
			ArrayList<Object> out = new ArrayList<Object>();
			ArrayList<Event> returnResult = new ArrayList<Event>();
			ArrayList<Event> FinalResult = new ArrayList<Event>();
			GetRow results = new GetRow();
			out = results.findObjectsByDateRange(limit, dateHigh, dateLow, ConfigData.EVENT_TABLE, "obj");
			for(int i = 0; i < out.size(); i++)
			{
				returnResult.add((Event) out.get(i));
			}
			if(returnResult.size() > 0){
				for(int i = 0; i < returnResult.size(); i++)
				{
					Event e; 
					int id = returnResult.get(i).getId();
					e = Event.find(id);
					FinalResult.add(e);
				}
			}
			return FinalResult;
		}
		
		public ArrayList<Milestone> getMilestone(int limit, String dateHigh, String dateLow)throws SQLException, IOException, NotFoundException, ClassNotFoundException 
		{
			ArrayList<Object> out = new ArrayList<Object>();
			ArrayList<Milestone> returnResult = new ArrayList<Milestone>();
			ArrayList<Milestone> FinalResult = new ArrayList<Milestone>();
			GetRow results = new GetRow();
			out = results.findObjectsByDateRange(limit, dateHigh, dateLow, ConfigData.MILESTONE_TABLE, "obj");
			for(int i = 0; i < out.size(); i++)
			{
				returnResult.add((Milestone) out.get(i));
			}
			if(returnResult.size() > 0){
				for(int i = 0; i < returnResult.size(); i++)
				{
					Milestone m; 
					int id = returnResult.get(i).getId();
					m = Milestone.find(id);
					FinalResult.add(m);
				}
			}
			return FinalResult;
		}
		
		public ArrayList<Person> getPerson(int limit, String dateHigh, String dateLow)throws SQLException, IOException, NotFoundException, ClassNotFoundException 
		{
			ArrayList<Object> out = new ArrayList<Object>();
			ArrayList<Person> returnResult = new ArrayList<Person>();
			ArrayList<Person> FinalResult = new ArrayList<Person>();
			GetRow results = new GetRow();
			out = results.findObjectsByDateRange(limit, dateHigh, dateLow, ConfigData.PERSON_TABLE, "obj");
			for(int i = 0; i < out.size(); i++)
			{
				returnResult.add((Person) out.get(i));
			}
			if(returnResult.size() > 0){
				for(int i = 0; i < returnResult.size(); i++)
				{
					Person p; 
					int id = returnResult.get(i).getId();
					p = Person.find(id);
					FinalResult.add(p);
				}
			}
			return FinalResult;
		}
		
		public ArrayList<Project> getProject(int limit, String dateHigh, String dateLow)throws SQLException, IOException, NotFoundException, ClassNotFoundException 
		{
			ArrayList<Object> out = new ArrayList<Object>();
			ArrayList<Project> returnResult = new ArrayList<Project>();
			ArrayList<Project> FinalResult = new ArrayList<Project>();
			GetRow results = new GetRow();
			out = results.findObjectsByDateRange(limit, dateHigh, dateLow, ConfigData.PROJECT_TABLE, "obj");
			for(int i = 0; i < out.size(); i++)
			{
				returnResult.add((Project) out.get(i));
			}
			if(returnResult.size() > 0){
				for(int i = 0; i < returnResult.size(); i++)
				{
					Project p; 
					int id = returnResult.get(i).getId();
					p = Project.find(id);
					FinalResult.add(p);
				}
			}
			return FinalResult;
		}
		
		public ArrayList<Publication> getPublication(int limit, String dateHigh, String dateLow)throws SQLException, IOException, NotFoundException, ClassNotFoundException 
		{
			ArrayList<Object> out = new ArrayList<Object>();
			ArrayList<Publication> returnResult = new ArrayList<Publication>();
			ArrayList<Publication> FinalResult = new ArrayList<Publication>();
			GetRow results = new GetRow();
			out = results.findObjectsByDateRange(limit, dateHigh, dateLow, ConfigData.PUBLICATION_TABLE, "obj");
			for(int i = 0; i < out.size(); i++)
			{
				returnResult.add((Publication) out.get(i));
			}
			if(returnResult.size() > 0){
				for(int i = 0; i < returnResult.size(); i++)
				{
					Publication p; 
					int id = returnResult.get(i).getId();
					p = Publication.find(id);
					FinalResult.add(p);
				}
			}
			return FinalResult;
		}
		
		public ArrayList<Deliverable> getDeliverable(int limit, String dateHigh, String dateLow)throws SQLException, IOException, NotFoundException, ClassNotFoundException 
		{
			ArrayList<Object> out = new ArrayList<Object>();
			ArrayList<Deliverable> returnResult = new ArrayList<Deliverable>();
			ArrayList<Deliverable> FinalResult = new ArrayList<Deliverable>();
			GetRow results = new GetRow();
			out = results.findObjectsByDateRange(limit, dateHigh, dateLow, ConfigData.DELIVERABLE_TABLE, "obj");
			for(int i = 0; i < out.size(); i++)
			{
				returnResult.add((Deliverable) out.get(i));
			}
			if(returnResult.size() > 0){
				for(int i = 0; i < returnResult.size(); i++)
				{
					Deliverable d; 
					int id = returnResult.get(i).getId();
					d = Deliverable.find(id);
					FinalResult.add(d);
				}
			}
			return FinalResult;
		}
}
